package com.ly.blog2Word;

import com.ly.blog2Common.core.http.LoadProperties;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;

/**
 * @CreateDate: 2018/12/13 22:49
 */


@EnableFeignClients
@EnableScheduling //开启定时器支持
@SpringBootApplication
//@MapperScan("com.ly.dao")
public class Blog2WordDemo extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Blog2WordDemo.class);
        // 加载默认错误码
        application.addListeners(new LoadProperties("errConfig.properties"));
        application.run(args);
    }

//    springboot打jar包正常无法访问页面
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        // 用于独立tomcat运行 ； 注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(Blog2WordDemo.class);
    }


    @Bean
    @LoadBalanced //使用ribbon 负载均衡 默认轮训
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }


    /**
     * 为了服务监控而配置，与容错本身无关
     * @return
     */
    @Bean
    public ServletRegistrationBean getServlet(){
        HystrixMetricsStreamServlet streamServlet =new HystrixMetricsStreamServlet();
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(streamServlet);
        servletRegistrationBean.setLoadOnStartup(1);
        servletRegistrationBean.addUrlMappings("/hystrix.stream");
        servletRegistrationBean.setName("HystrixMetricsStreamServlet");
        return servletRegistrationBean;
    }
}
